import os.path

from PySide2 import QtWidgets
from PySide2.QtWidgets import QFileDialog, QDialogButtonBox, QMessageBox

from ui.dialog.settings_dialog import Ui_SettingsDialog
from utils.file_utils import Config, empty_folder


class Settings(QtWidgets.QDialog, Ui_SettingsDialog):

    def __init__(self, parent=None):
        super(Settings, self).__init__(parent)
        self.setupUi(self)
        self.parent = parent
        self.pushButtonSelectImgPath.clicked.connect(self.select_img_save_path)
        self.pushButtonDelete.clicked.connect(self.delete_rk_imgs)
        self.pushButtonSelSDKPath.clicked.connect(self.select_sdk_save_path)
        self.pushButtonDelSDK.clicked.connect(self.delete_sdk_pkgs)
        self.pushButtonSelIDEPath.clicked.connect(self.select_ide_config_path)
        # 系统按钮
        self.buttonBox.button(QDialogButtonBox.Ok).setText('确认')
        self.buttonBox.button(QDialogButtonBox.Cancel).setText('取消')

    def reload_settings(self):
        # 全局设置
        config_data = Config.read_config()
        self.lineEditDayu200SavePath.setText(config_data['dayu200_path'])
        self.lineEditSDKSavePath.setText(config_data['sdk_path'])
        self.lineEditIDEConfig.setText(config_data['ide_path'])
        # 任务没结束，不允许修改和删除缓存
        if self.parent.running:
            self.groupBoxGlobal.setEnabled(False)
            self.groupBoxRK3568.setEnabled(False)

    def select_img_save_path(self):
        answer = QFileDialog.getExistingDirectory(self, '文件夹', self.lineEditDayu200SavePath.text())
        if not answer:
            return
        self.lineEditDayu200SavePath.setText(os.path.realpath(answer))

    def delete_rk_imgs(self):
        answer = QMessageBox.warning(self, self.windowTitle(), '确定要删除所有下载的镜像吗？', QMessageBox.Yes | QMessageBox.Cancel)
        if answer != QMessageBox.Yes:
            return
        self.pushButtonDelete.setEnabled(False)
        try:
            empty_folder(self.lineEditDayu200SavePath.text())
        except:
            raise
        finally:
            self.pushButtonDelete.setEnabled(True)

    def select_sdk_save_path(self):
        answer = QFileDialog.getExistingDirectory(self, '文件夹', self.lineEditSDKSavePath.text())
        if not answer:
            return
        self.lineEditSDKSavePath.setText(os.path.realpath(answer))

    def delete_sdk_pkgs(self):
        answer = QMessageBox.warning(self, self.windowTitle(), '确定要删除所有下载的SDK包吗？', QMessageBox.Yes | QMessageBox.Cancel)
        if answer != QMessageBox.Yes:
            return
        self.pushButtonDelSDK.setEnabled(False)
        try:
            empty_folder(self.lineEditSDKSavePath.text())
        except:
            raise
        finally:
            self.pushButtonDelSDK.setEnabled(True)

    def select_ide_config_path(self):
        answer = QFileDialog.getExistingDirectory(self, '文件夹', self.lineEditIDEConfig.text())
        if not answer:
            return
        self.lineEditIDEConfig.setText(os.path.realpath(answer))

    def accept(self) -> None:
        # 任务没结束，直接关闭
        if self.parent.running:
            super().accept()
            return
        # 任务没结束不允许修改保存路径
        config_data = {
            'dayu200_path': self.lineEditDayu200SavePath.text().strip(),
            'sdk_path': self.lineEditSDKSavePath.text().strip(),
            'ide_path': self.lineEditIDEConfig.text().strip(),
        }
        Config.save_config(config_data)
        super().accept()
